var DataProxy = require("data-proxy");
var Action = require("../apiaction");
var day = 1000*60*60*24;
exports.loginRouter = function(router){

    //登录页面
    //router.get(['/','/login'], function(req, res) {
    //    var sessionid   = req.cookies.sessionid;
    //    //if(sessionid) {
    //    //    Action.send(req, res,"check",{sessionid:sessionid},function(res,data){
    //    //        var json = JSON.parse(data);
    //    //        if(json.code == 1 ){
    //    //            res.render('login/login',{layout:'login-layout'});
    //    //        }else {
    //    //            res.redirect('/pam/index');
    //    //        }
    //    //    });
    //    //}else {
    //    //    res.render('login/login',{layout:'login-layout'});
    //    //}
    //    res.render('login/login',{
    //        layout  : 'layout-login',
    //        jscript : 'login/login'
    //    });
    //});

    //注册页面
    router.get('/regist',function(req,res){
        res.render ('login/register',{layout:'login-layout',jscript:'register'});
    });

    router.get('/quite', function(req, res) {
        console.log("退出成功");
        res.clearCookie('user');
        res.redirect('/');
    });

    //找回密码页面
    router.get('/findpw',function(req,res){
        res.render('login/findpw');

    });

    //进入手机找回密码
    router.get('/findbyphone',function(req,res) {
        res.render ('login/findpwbyphone');
    });

    //邮箱找回密码
    router.get('/findbyemail',function(req,res) {
        res.render ('login/findpwbyemail');
    });
    //邮箱找回密码2
    router.get('/resetpwdbyemail',function(req,res) {
        res.render ('login/repwbyemail');
    });

    //找回密码通过手机
    router.post('/doFindpw',function(req,res){
        var mobile=req.param("mobile");
        var findmethod=req.param("findmethod");

        //console.log(findmethod);
        //console.log(mobile);
        new DataProxy({
            req:req,
            res:res,
            reqType:"http",
            reqOption:{
                url:"/login/findMethods",
                params:{mobile:mobile,methods:findmethod},
                success:function(res,data){
                    var json = JSON.parse(data);

                    if(json.code == 0 ){
                        if(json.data == 1){
                            json.data = {redirectUrl:"/findbyphone?mobile="+mobile};
                        }else if(json.data = 2){
                            json.data = {redirectUrl:"/findbyemail?mobile="+mobile};
                        }


                        res.status(200).send(JSON.stringify(json));

                    }else{
                        json.data = {field:"mobile"};
                        res.status(200).send(JSON.stringify(json));
                    }
                }
            }
        }).handleRequest();
    });


    //找回密码通过手机2--修改密码
    router.post('/doFindpwbyphone',function(req,res){
        var mobile=req.param("mobile");
        var password=req.param("password");
        var captcha = req.param("captcha");

        new DataProxy({
            req:req,
            res:res,
            reqType:"http",
            reqOption:{
                url:"/login/mobileMethods",
                params:{mobile:mobile,password:password,captcha:captcha},
                success:function(res,data){
                    var json = JSON.parse(data);
                    if(json.code == 0 ){
                        json.data = {redirectUrl:"/"};
                        res.status(200).send(JSON.stringify(json));
                    }else{
                        res.status(200).send(data);
                    }
                }
            }
        }).handleRequest();
    });

    //找回密码通过邮箱2--修改密码
    router.post('/doFindpwbyemail',function(req,res){
        var mobile=req.param("mobile");
        var email=req.param("email");

        new DataProxy({
            req:req,
            res:res,
            reqType:"http",
            reqOption:{
                url:"/login/emailMethods",
                params:{mobile:mobile,email:email},
                success:function(res,data){
                    var json = JSON.parse(data);
                    if(json.code == 0 ){
                        //json.data = {redirectUrl:"/"};

                        res.status(200).send(JSON.stringify(json));
                    }else{
                        res.status(200).send({code:1,msg:json.msg,data:{field:'email'}});
                    }
                }
            }
        }).handleRequest();
    });

    //发送短信
    router.post('/sendCaptcha',function(req,res){
        var mobile=req.param("mobile");

        new DataProxy({
            req:req,
            res:res,
            reqType:"http",
            reqOption:{
                url:"/login/resendCaptcha",
                params:{mobile:mobile},
                success:function(res,data){
                    var json = JSON.parse(data);
                    if(json.code == 0 ){

                        res.status(200).send(JSON.stringify(json));
                    }else{
                        res.status(200).send({code:1,msg:json.msg,data:{field:'mobile'}});
                    }
                }
            }
        }).handleRequest();
    });

    //邮件重置密码
    router.post('/doResetByMail',function(req,res){
        var mobile=req.param("mobile");
        var password=req.param("password");
        var t = req.param("t");

        new DataProxy({
            req:req,
            res:res,
            reqType:"http",
            reqOption:{
                url:"/login/resetPwdByMail",
                params:{mobile:mobile,password:password,t:t},
                success:function(res,data){
                    var json = JSON.parse(data);
                    if(json.code == 0 ){
                        json.data = {redirectUrl:"/"};

                        res.status(200).send(JSON.stringify(json));
                    }else{
                        res.status(200).send({code:1,msg:json.msg,data:{field:'mobile'}});
                    }
                }
            }
        }).handleRequest();
    });
    //登录
    router.post('/doLogin',function(req,res){
        var login_name = req.param("login_name");
        var password = req.param("password");
        var remember = "true";

        console.log(login_name,password,remember)
        new DataProxy({
            req:req,
            res:res,
            reqType:"http",
            reqOption:{
                url:"/adminUser/dologin",
                params:{user_name:login_name,password:password,remember:remember},
                success:function(res,data){
                    var json = JSON.parse(data);
                    console.log("doLogin result:",json)
                    if(json.code == 0 ){
                        //send sessionid to client as cookie
                        if(remember == 'true') {
                            res.cookie('sessionid',json.data.session_id,{path: '/',expires: new Date(Date.now() + (3*day))});
                            res.cookie('user_name',json.data.user_name,{path: '/',expires: new Date(Date.now() + (3*day))});
                            res.cookie('real_name',json.data.real_name,{path: '/',expires: new Date(Date.now() + (3*day))});

                        }else {
                            res.cookie('sessionid', json.data.session_id, {
                                path: '/',
                                expires: new Date(Date.now() + day)
                            });

                            res.cookie('user_name', json.data.user_name, {
                                path: '/',
                                expires: new Date(Date.now() + day)
                            });

                            res.cookie('real_name', json.data.real_name, {
                                path: '/',
                                expires: new Date(Date.now() + day)
                            });

                        }

                        res.status(200).send(JSON.stringify(json));
                    }
                    else{
                        res.status(200).send(data);
                    }
                }
            }
        }).handleRequest();
    });

    //注册
    router.post('/doRegist',function(req,res){
        var mobile = req.param("mobile");
        var password = req.param("password");
        var email = req.param("email");
        var login_name = req.param("login_name");
        new DataProxy({
            req:req,
            res:res,
            reqType:"http",
            reqOption:{
                url:"/login/doRegist",
                params:{mobile:mobile,password:password,email:email,login_name:login_name},
                success:function(res,data){
                    var json = JSON.parse(data);
                    if(json.code == 0 ){
                        //send sessionid to client as cookie
                        res.cookie('sessionid',json.data.sessionid,{path: '/',expires: new Date(Date.now() + day)});
                        res.cookie('usermobile',json.data.mobile,{path: '/',expires: new Date(Date.now() + day)});
                        res.cookie('username',json.data.login_name,{path: '/',expires: new Date(Date.now() + day)});
                        res.cookie('userid',json.data.user_id,{path: '/',expires: new Date(Date.now() + day)});
                        res.cookie('createdDate',json.data.createdDate,{path: '/',expires: new Date(Date.now() + day)});
                        res.cookie('portrait',json.data.portrait,{path: '/',expires: new Date(Date.now() + day)});
                        res.status(200).send(JSON.stringify(json));
                    }else{
                        res.status(200).send(data);
                    }
                }
            }
        }).handleRequest();
    });


    router.get('/logout',function(req,res) {
        var sessionid = req.cookies.sessionid;
        new DataProxy({
            req: req,
            res: res,
            reqType: "http",
            reqOption: {
                url: "/adminUser/logout",
                params: {sessionid: sessionid},
                success: function (res, data) {
                    res.clearCookie('sessionid', { path: '/' });
                    res.clearCookie('userid', { path: '/' });
                    res.clearCookie('username', { path: '/' });
                    res.clearCookie('usermobile', { path: '/' });
                    res.clearCookie('createdDate', { path: '/' });
                    res.clearCookie('portrait', { path: '/' });
                    res.redirect("/login");

                }
            }
        }).handleRequest();
    });


    router.all('/doEditPwd', function(req, res) {
        var password = req.param("password");
        var oldpassword = req.param("oldpassword");
        var sessionid = req.cookies.sessionid;

        new DataProxy({
            req:req,
            res:res,
            reqType:"http",
            reqOption:{
                url:"/login/updateUserPwd",
                params:{oldPwd:oldpassword,newPwd:password,sessionid:sessionid},
                success:function(res,d){
                    res.status(200).send(d);
                }
            }
        }).handleRequest();

    });

}